package view.normalview;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridLayout;

import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.ComboBoxModel;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import Model.Association;
import Model.Classe;
import Model.diagram.CombinationDiagrame;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.event.InternalFrameListener;

import view.interfaces.Iframe;
import view.interfaces.Observer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class DrawPanel extends JPanel implements Iframe{
	private JLabel label;
	private JLabel label_1;
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private boolean editpanel=false;
	private JTextField textField;
	private JButton btnOk;
	private CombinationDiagrame resultdia=new CombinationDiagrame();
	private Observer controll;
	private JLabel lblOp;
	private JLabel lblResult;
	private JPanel panel;
	private JButton btnGenerateReport;
	/**
	 * Create the panel.
	 */
	public DrawPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{308, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		label = new JLabel("rebs");
	
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.WEST;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 0;
		gbc_label.gridy = 0;
		add(label, gbc_label);
		
		lblResult = new JLabel("result");
		GridBagConstraints gbc_lblResult = new GridBagConstraints();
		gbc_lblResult.insets = new Insets(0, 0, 5, 5);
		gbc_lblResult.gridx = 2;
		gbc_lblResult.gridy = 1;
		add(lblResult, gbc_lblResult);
		
	
		
		label_1 = new JLabel("New label");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 0;
		gbc_label_1.gridy = 2;
		add(label_1, gbc_label_1);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
			}
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 3;
		add(textField, gbc_textField);
		textField.setColumns(10);
		
		btnOk = new JButton("ok");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
Association temp=new Association();	
temp.addUpper(new Classe(comboBox.getSelectedItem().toString()));
temp.addUpper(new Classe(comboBox_1.getSelectedItem().toString()));
temp.addFunctionality(textField.getText());
resultdia.addAssociation(temp);
update();
				
			}
		});
		GridBagConstraints gbc_btnOk = new GridBagConstraints();
		gbc_btnOk.insets = new Insets(0, 0, 5, 5);
		gbc_btnOk.gridx = 4;
		gbc_btnOk.gridy = 3;
		add(btnOk, gbc_btnOk);
		
		btnGenerateReport = new JButton("generate report");
		btnGenerateReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			setReport();
			}
		});
		GridBagConstraints gbc_btnGenerateReport = new GridBagConstraints();
		gbc_btnGenerateReport.insets = new Insets(0, 0, 5, 5);
		gbc_btnGenerateReport.gridx = 5;
		gbc_btnGenerateReport.gridy = 3;
		add(btnGenerateReport, gbc_btnGenerateReport);
		
		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 4;
		add(panel, gbc_panel);
		
		

	}

	public JLabel getLabelup() {
		return label;
	}
	public JLabel getLabeldown() {
		return label_1;
	}
	public void setUpper(ImageIcon i){
		label.setIcon(i);
	}
	public void setLower(ImageIcon i){
		label_1.setIcon(i);
	}
	public void setupperlist(Map<String,Classe> list){
		
		ArrayList<String> box=new ArrayList<String>();
		for(Map.Entry<String, Classe> mapa: list.entrySet()){
			box.add(mapa.getValue().getName());
			
		}
		
		comboBox_1 = new JComboBox<Object>(box.toArray());
		GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
		gbc_comboBox_1.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_1.gridx = 0;
		gbc_comboBox_1.gridy = 3;
		add(comboBox_1, gbc_comboBox_1);
		
	}

public void setlowerlist(Map<String,Classe> list){
	ArrayList<String> box=new ArrayList<String>();
	for(Map.Entry<String, Classe> mapa: list.entrySet()){
		box.add(mapa.getValue().getName());
		
	}
	
	
	comboBox = new JComboBox(box.toArray());
	GridBagConstraints gbc_comboBox = new GridBagConstraints();
	gbc_comboBox.insets = new Insets(0, 0, 5, 5);
	gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
	gbc_comboBox.gridx = 2;
	gbc_comboBox.gridy = 3;
	add(comboBox, gbc_comboBox);
	}
	
	public void  setImageIcon(){
		
	}
	public JComboBox getComboBox_1() {
		return comboBox_1;
	}
	public JTextField getTextField() {
		return textField;
	}
	public JComboBox getComboBox() {
		return comboBox;
	}

	@Override
	public void update() {
		
		controll.update(resultdia);
		setReport();
	}

	@Override
	public void add(Observer o) {
		this.controll=o;
		
	}
	
	public JLabel getLblOp() {
		return lblOp;
	}
	public void setResult(ImageIcon i){
		lblResult.setIcon(new ImageIcon(i.getImage()));
		lblResult.repaint();
		
		
	}
	public JLabel result() {
		return lblResult;
	}
	public JPanel getPanel() {
		return panel;
	}
	public void setReport(){
		finalReport report=new finalReport(resultdia);
		
		panel.removeAll();
		panel.add(report);
		this.repaint();
	}
}
